import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../components/status/toast.dart';

class ExitPrompt extends StatelessWidget {
  // 存储操作时间差（用于判断点击系统返回按钮是否退出系统）
  DateTime _lastQuitTime;

  // 组件
  Widget child;

  /// 退出提示
  Future<bool> exit() async {
    if (_lastQuitTime == null ||
        DateTime.now().difference(_lastQuitTime).inSeconds > 1) {
      Toast.toast(
        title: "再按一次退出系统",
        position: EasyLoadingToastPosition.bottom,
      );
      _lastQuitTime = DateTime.now();
      return false;
    }
    return true;
  }

  ExitPrompt({@required Widget child}) : this.child = child;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => exit(),
      child: child,
    );
  }
}
